{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "89fd188c",
   "metadata": {},
   "source": [
    "# 习题2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a0741580",
   "metadata": {},
   "source": [
    "**<font size=5>3.</font>**设$A=\\begin{pmatrix} a_{11} & a_{12} \\\\ a_{21} & a_{22} \\\\ a_{31} & a_{32} \\end{pmatrix}$,$B=\\begin{pmatrix} b_1 & 0 \\\\ 0 & b_2 \\end{pmatrix}$,$C=\\begin{pmatrix} c_1 & 0 & 0 \\\\ 0 & c_2 & 0 \\\\ 0 & 0 & c_3 \\end{pmatrix}$,求$AB$和$CA$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "41297baa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}a_{11} b_{1} & a_{12} b_{2}\\\\a_{21} b_{1} & a_{22} b_{2}\\\\a_{21} b_{1} & a_{32} b_{2}\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[a11*b1, a12*b2],\n",
       "[a21*b1, a22*b2],\n",
       "[a21*b1, a32*b2]])"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import sympy as sp\n",
    "A = np.array([['a11','a12'],['a21','a22'],['a21','a32']])\n",
    "B = np.array([['b1',0],[0,'b2']])\n",
    "C = np.array([['c1',0,0],[0,'c2',0],[0,0,'c3']])\n",
    "\n",
    "A=sp.Matrix(A)\n",
    "B=sp.Matrix(B)\n",
    "C=sp.Matrix(C)\n",
    "A*B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "e3340613",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}a_{11} c_{1} & a_{12} c_{1}\\\\a_{21} c_{2} & a_{22} c_{2}\\\\a_{21} c_{3} & a_{32} c_{3}\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[a11*c1, a12*c1],\n",
       "[a21*c2, a22*c2],\n",
       "[a21*c3, a32*c3]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "C*A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a5a09ddb",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "1fc97835",
   "metadata": {},
   "source": [
    "**<font size=5>4.</font>**设$A=\\begin{pmatrix} 1 & 1 & 1 \\\\ 1 & 1 & -1 \\\\ 1 & -1 & 1 \\end{pmatrix}$,$B=\\begin{pmatrix} 1 & 2 & 3 \\\\ -1 & -2 & -4 \\\\ 0 & 5 & 1 \\end{pmatrix}$,求$3AB-2A$及$A^TB$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "39dd8319",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}-2 & 13 & 22\\\\-2 & -17 & 20\\\\4 & 29 & -2\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[-2,  13, 22],\n",
       "[-2, -17, 20],\n",
       "[ 4,  29, -2]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import sympy as sp\n",
    "A = np.array([[1,1,1],[1,1,-1],[1,-1,1]])\n",
    "B = np.array([[1,2,3],[-1,-2,4],[0,5,1]])\n",
    "\n",
    "A=sp.Matrix(A)\n",
    "B=sp.Matrix(B)\n",
    "3*A*B-2*A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "2ba55540",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}0 & 5 & 8\\\\0 & -5 & 6\\\\2 & 9 & 0\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[0,  5, 8],\n",
       "[0, -5, 6],\n",
       "[2,  9, 0]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A.T*B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "44322205",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "2ca3ab94",
   "metadata": {},
   "source": [
    "**<font size=5>5.</font>**计算下列乘积.\n",
    "(1)$\\begin{pmatrix} 4 & 3 & 1 \\\\ 1 & -2 & 3 \\\\ 5 & 7 & 0 \\end{pmatrix}$$\\begin{pmatrix} 7 \\\\ 2 \\\\ 1 \\end{pmatrix}$；(2)$\\begin{pmatrix} 1 & 2 & 3 \\end{pmatrix}$$\\begin{pmatrix} 3 \\\\ 2 \\\\ 1 \\end{pmatrix}$；(3)$\\begin{pmatrix} 2 \\\\ 1 \\\\ 3 \\end{pmatrix}$$\\begin{pmatrix} -1 & -2 \\end{pmatrix}$；(4)$\\begin{pmatrix} x_1 & x_2 & x_3 \\end{pmatrix}$$\\begin{pmatrix} a_{11} & a_{12} & a_{13} \\\\ a_{21} & a_{22} & a_{23} \\\\ a_{31} & a_{32} & a_{33} \\end{pmatrix}$$\\begin{pmatrix} x_1 \\\\ x_2 \\\\ x_3 \\end{pmatrix}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "3d388820",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}35\\\\6\\\\49\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[35],\n",
       "[ 6],\n",
       "[49]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#(1)\n",
    "import numpy as np\n",
    "import sympy as sp\n",
    "A = np.array([[4,3,1],[1,-2,3],[5,7,0]])\n",
    "B = np.array([[7],[2],[1]])\n",
    "\n",
    "A=sp.Matrix(A)\n",
    "B=sp.Matrix(B)\n",
    "A*B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "4fcc2e39",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}10\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([[10]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#(2)\n",
    "import numpy as np\n",
    "import sympy as sp\n",
    "A = np.array([[1,2,3]])\n",
    "B = np.array([[3],[2],[1]])\n",
    "\n",
    "A=sp.Matrix(A)\n",
    "B=sp.Matrix(B)\n",
    "A*B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "a9b854cd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}-2 & -4\\\\-1 & -2\\\\-3 & -6\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[-2, -4],\n",
       "[-1, -2],\n",
       "[-3, -6]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#(3)\n",
    "import numpy as np\n",
    "import sympy as sp\n",
    "A = np.array([[-1,-2]])\n",
    "B = np.array([[2],[1],[3]])\n",
    "\n",
    "A=sp.Matrix(A)\n",
    "B=sp.Matrix(B)\n",
    "B*A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "211b13b3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}x_{1} \\left(a_{11} x_{1} + a_{21} x_{2} + a_{31} x_{3}\\right) + x_{2} \\left(a_{12} x_{1} + a_{22} x_{2} + a_{32} x_{3}\\right) + x_{3} \\left(a_{13} x_{1} + a_{23} x_{2} + a_{33} x_{3}\\right)\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([[x_1*(a_11*x_1 + a_21*x_2 + a_31*x_3) + x_2*(a_12*x_1 + a_22*x_2 + a_32*x_3) + x_3*(a_13*x_1 + a_23*x_2 + a_33*x_3)]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#(4)\n",
    "import numpy as np\n",
    "import sympy as sp\n",
    "A = np.array([['x_1','x_2','x_3']])\n",
    "B = np.array([['a_11','a_12','a_13'],['a_21','a_22','a_23'],['a_31','a_32','a_33']])\n",
    "\n",
    "A=sp.Matrix(A)\n",
    "B=sp.Matrix(B)\n",
    "A*B*A.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "723b626c",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "23c7128c",
   "metadata": {},
   "source": [
    "**<font size=5>6.</font>**设$A=\\begin{pmatrix} 1 & 3 \\\\ 0 & 1 \\end{pmatrix}$，求$A^n$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "c1d5b3b6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}1 & 6\\\\0 & 1\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[1, 6],\n",
       "[0, 1]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import sympy as sp\n",
    "A = np.array([[1,3],[0,1]])\n",
    "A=sp.Matrix(A)\n",
    "A*A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "4451993e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}1 & 9\\\\0 & 1\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[1, 9],\n",
       "[0, 1]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A*A*A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "462a17e2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}1 & 12\\\\0 & 1\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[1, 12],\n",
       "[0,  1]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A*A*A*A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "517abb8c",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "21051249",
   "metadata": {},
   "source": [
    "**<font size=5>8.</font>**设$A=\\begin{pmatrix} 2 & 1 \\\\ -1 & 2 \\end{pmatrix}$，矩阵$B$满足$BA=B+2E$，求$|B|$\n",
    "\n",
    "解析：\n",
    "$B=2(A-E)^{-1}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "5a7756fe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}1 & -1\\\\1 & 1\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[1, -1],\n",
       "[1,  1]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import sympy as sp\n",
    "A = np.array([[2,1],[-1,2]])\n",
    "E = np.array([[1,0],[0,1]])\n",
    "\n",
    "A=sp.Matrix(A)\n",
    "E=sp.Matrix(E)\n",
    "2*(A-E).inv()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d45224a7",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "32aa980e",
   "metadata": {},
   "source": [
    "**<font size=5>10.</font>**求下列矩阵的逆矩阵\n",
    "\n",
    "(1)$\\begin{pmatrix} 1 & 2 \\\\ 2 & 5 \\end{pmatrix}$ ;(2)$\\begin{pmatrix} cos\\theta & -sin\\theta \\\\ sin\\theta & cos\\theta \\end{pmatrix}$;(3)$\\begin{pmatrix} 1 & 2 & 1 \\\\ 3 & 4 & 2 \\\\ 5 & -4 & 1 \\end{pmatrix}$\n",
    "\n",
    "(4)$\\begin{pmatrix} 1 & 0 & 0 & 0 \\\\ 1 & 2 & 0 & 0 \\\\ 2 & 2 & 3 & 0 \\\\ 1 & 2 & 0 & 4 \\end{pmatrix}$; \n",
    "(5)$\\begin{pmatrix} 1 & 2 & 3 & 4 \\\\ 0 & 1 & 2 & 3 \\\\ 0 & 0 & 1 & 2 \\\\ 0 & 0 & 0 & 1 \\end{pmatrix}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "c87274e8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}5 & -2\\\\-2 & 1\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[ 5, -2],\n",
       "[-2,  1]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#(1)\n",
    "import numpy as np\n",
    "import sympy as sp\n",
    "A = np.array([[1,2],[2,5]])\n",
    "A=sp.Matrix(A)\n",
    "\n",
    "A.inv()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "f583e229",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}- \\frac{\\sin^{2}{\\left(\\theta \\right)}}{\\cos{\\left(\\theta \\right)}} + \\frac{1}{\\cos{\\left(\\theta \\right)}} & \\sin{\\left(\\theta \\right)}\\\\- \\sin{\\left(\\theta \\right)} & \\cos{\\left(\\theta \\right)}\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[-sin(theta)**2/cos(theta) + 1/cos(theta), sin(theta)],\n",
       "[                             -sin(theta), cos(theta)]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#(2)\n",
    "import numpy as np\n",
    "import sympy as sp\n",
    "A = np.array([['cos(theta)','-sin(theta)'],['sin(theta)','cos(theta)']])\n",
    "A=sp.Matrix(A)\n",
    "\n",
    "A.inv()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "a7d8d18e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}-2 & 1 & 0\\\\- \\frac{7}{6} & \\frac{2}{3} & - \\frac{1}{6}\\\\\\frac{16}{3} & - \\frac{7}{3} & \\frac{1}{3}\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[  -2,    1,    0],\n",
       "[-7/6,  2/3, -1/6],\n",
       "[16/3, -7/3,  1/3]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#(3)\n",
    "import numpy as np\n",
    "import sympy as sp\n",
    "A = np.array([[1,2,1],[3,4,2],[5,-4,1]])\n",
    "A=sp.Matrix(A)\n",
    "\n",
    "A.inv()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "c02309bb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}1 & 0 & 0 & 0\\\\- \\frac{1}{2} & \\frac{1}{2} & 0 & 0\\\\- \\frac{1}{3} & - \\frac{1}{3} & \\frac{1}{3} & 0\\\\0 & - \\frac{1}{4} & 0 & \\frac{1}{4}\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[   1,    0,   0,   0],\n",
       "[-1/2,  1/2,   0,   0],\n",
       "[-1/3, -1/3, 1/3,   0],\n",
       "[   0, -1/4,   0, 1/4]])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#(4)\n",
    "import numpy as np\n",
    "import sympy as sp\n",
    "A = np.array([[1,0,0,0],[1,2,0,0],[2,2,3,0],[1,2,0,4]])\n",
    "A=sp.Matrix(A)\n",
    "\n",
    "A.inv()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "38c5f4ba",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}1 & -2 & 1 & 0\\\\0 & 1 & -2 & 1\\\\0 & 0 & 1 & -2\\\\0 & 0 & 0 & 1\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[1, -2,  1,  0],\n",
       "[0,  1, -2,  1],\n",
       "[0,  0,  1, -2],\n",
       "[0,  0,  0,  1]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#(5)\n",
    "import numpy as np\n",
    "import sympy as sp\n",
    "A = np.array([[1,2,3,4],[0,1,2,3],[0,0,1,2],[0,0,0,1]])\n",
    "A=sp.Matrix(A)\n",
    "\n",
    "A.inv()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b7db7b37",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "e61b4875",
   "metadata": {},
   "source": [
    "**<font size=5>11.</font>**用逆矩阵求解下列矩阵方程.\n",
    "\n",
    "(1)$\\begin{pmatrix} 2 & 5 \\\\ 1 & 3 \\end{pmatrix}X=\\begin{pmatrix} 4 & -6 \\\\ 2 & 1 \\end{pmatrix}$ ;(2)$X\\begin{pmatrix} 2 & 1 & -1\\\\ 2 & 1 & 0\\\\ 1 & -1 & 1\\end{pmatrix}=\\begin{pmatrix} 1 & -1 & 3\\\\ 4 & 3 & 2\\end{pmatrix}$\n",
    "\n",
    "(3)$\\begin{pmatrix} 1 & 4 \\\\ -1 & 2 \\end{pmatrix}X\\begin{pmatrix} 2 & 0 \\\\ -1 & 1 \\end{pmatrix}=\\begin{pmatrix} 3 & 1 \\\\ 0 & -1 \\end{pmatrix}$ "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "dc8be4ef",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}2 & -23\\\\0 & 8\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[2, -23],\n",
       "[0,   8]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#(1)\n",
    "import numpy as np\n",
    "import sympy as sp\n",
    "A = np.array([[2,5],[1,3]])\n",
    "B = np.array([[4,-6],[2,1]])\n",
    "A=sp.Matrix(A)\n",
    "B=sp.Matrix(B)\n",
    "\n",
    "A.inv()*B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "1a48394b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}-2 & 2 & 1\\\\- \\frac{8}{3} & 5 & - \\frac{2}{3}\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[  -2, 2,    1],\n",
       "[-8/3, 5, -2/3]])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#(2)\n",
    "import numpy as np\n",
    "B = np.array([[1,-1,3],[4,3,2]])\n",
    "A = np.array([[2,1,-1],[2,1,0],[1,-1,1]])\n",
    "A=sp.Matrix(A)\n",
    "B=sp.Matrix(B)\n",
    "B*A.inv()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "5bc4b72a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}1 & 1\\\\\\frac{1}{4} & 0\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[  1, 1],\n",
       "[1/4, 0]])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#(3)\n",
    "import numpy as np\n",
    "import sympy as sp\n",
    "A = np.array([[1,4],[-1,2]])\n",
    "B = np.array([[2,0],[-1,1]])\n",
    "C = np.array([[3,1],[0,-1]])\n",
    "A=sp.Matrix(A)\n",
    "B=sp.Matrix(B)\n",
    "C=sp.Matrix(C)\n",
    "\n",
    "A.inv()*C*B.inv()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4db8e4d9",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "7483919e",
   "metadata": {},
   "source": [
    "**<font size=5>12.</font>**利用矩阵解下列线性方程.\n",
    "\n",
    "(1)$\\begin{equation*}\n",
    "     \\begin{cases}\n",
    "         x_1+2x_2+3x_3=1 \\\\\n",
    "         2x_1+2x_2+5x_3=2 \\\\ \n",
    "         3x_1+5x_2+x_3=3\n",
    "     \\end{cases}\n",
    " \\end{equation*}$；\n",
    " (2)$\\begin{equation*}\n",
    "     \\begin{cases}\n",
    "         x_1-x_2-x_3=2 \\\\\n",
    "         2x_1-x_2-3x_3=1 \\\\ \n",
    "         3x_1+2x_2-5x_3=0\n",
    "     \\end{cases}\n",
    " \\end{equation*}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "d39b1dac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left\\{\\left( 1, \\  0, \\  0\\right)\\right\\}$"
      ],
      "text/plain": [
       "{(1, 0, 0)}"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#(1)\n",
    "from sympy import *\n",
    "x, y, z = symbols(\"x y z\")\n",
    "eq = Matrix(([1, 2, 3, 1], [2, 2,5, 2], [3, 5, 1, 3]))\n",
    "result = linsolve(eq, [x, y, z])\n",
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "d5a79ce0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left\\{\\left( 5, \\  0, \\  3\\right)\\right\\}$"
      ],
      "text/plain": [
       "{(5, 0, 3)}"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#(2)\n",
    "from sympy import *\n",
    "x, y, z = symbols(\"x y z\")\n",
    "eq = Matrix(([1, -1, -1, 2], [2, -1,-3, 1], [3, 2, -5, 0]))\n",
    "result = linsolve(eq, [x, y, z])\n",
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2a0ffba6",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "a6c1837e",
   "metadata": {},
   "source": [
    "**<font size=5>15.</font>**设$A=\\begin{pmatrix} 0 & 3 & 3 \\\\ 1 & 1 & 0 \\\\ -1 & 2 & 3 \\end{pmatrix}$，$AB=A+2B$，求$B$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "3087912f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}0 & 3 & 3\\\\-1 & 2 & 3\\\\1 & 1 & 0\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[ 0, 3, 3],\n",
       "[-1, 2, 3],\n",
       "[ 1, 1, 0]])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import sympy as sp\n",
    "A = np.array([[0,3,3],[1,1,0],[-1,2,3]])\n",
    "E = sp.eye(3)\n",
    "A=sp.Matrix(A)\n",
    "\n",
    "B=(A-2*E).inv()*A\n",
    "B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0e22d0b1",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "c530b72e",
   "metadata": {},
   "source": [
    "**<font size=5>16.</font>**求下列三角阵$A$与$B$的乘积，其中\n",
    "\n",
    "$A=\\begin{pmatrix} 1 & 0 & 0 & 0 \\\\ 0 & 1 & 0 & 0 \\\\ -2 & 2 & 1 & 0 \\\\ 3 & 0 & -1 & 1 \\end{pmatrix}$，$B=\\begin{pmatrix} 1 & 0 & 0 & 0 \\\\ 3 & 2 & 0 & 0 \\\\ 2 & -1 & 1 & 0 \\\\ -1 & 2 & 0 & 1 \\end{pmatrix}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "74a8610e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}1 & 0 & 0 & 0\\\\3 & 2 & 0 & 0\\\\6 & 3 & 1 & 0\\\\0 & 3 & -1 & 1\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[1, 0,  0, 0],\n",
       "[3, 2,  0, 0],\n",
       "[6, 3,  1, 0],\n",
       "[0, 3, -1, 1]])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import sympy as sp\n",
    "A = np.array([[1,0,0,0],[0,1,0,0],[-2,2,1,0],[3,0,-1,1]])\n",
    "B = np.array([[1,0,0,0],[3,2,0,0],[2,-1,1,0],[-1,2,0,1]])\n",
    "\n",
    "A=sp.Matrix(A)\n",
    "B=sp.Matrix(B)\n",
    "\n",
    "A*B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8f3e80c1",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "eea5125b",
   "metadata": {},
   "source": [
    "**<font size=5>17.</font>**设矩阵$A=\\begin{pmatrix} 1 & 0 & 2 & 3 \\\\ 0 & 1 & 1 & 4 \\\\ 0 & 0 & 1 & 0 \\\\ 0 & 0 & 0 & -1 \\end{pmatrix}$，$B=\\begin{pmatrix} 1 & 0 & 0 & 0 \\\\ 0 & 1 & 0 & 0 \\\\ 6 & 3 & 1 & 2 \\\\ 0 & -2 & 2 & 0 \\end{pmatrix}$，求$AB$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "600b3c00",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}13 & 0 & 8 & 4\\\\6 & -4 & 9 & 2\\\\6 & 3 & 1 & 2\\\\0 & 2 & -2 & 0\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[13,  0,  8, 4],\n",
       "[ 6, -4,  9, 2],\n",
       "[ 6,  3,  1, 2],\n",
       "[ 0,  2, -2, 0]])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import sympy as sp\n",
    "A = np.array([[1,0,2,3],[0,1,1,4],[0,0,1,0],[0,0,0,-1]])\n",
    "B = np.array([[1,0,0,0],[0,1,0,0],[6,3,1,2],[0,-2,2,0]])\n",
    "\n",
    "A=sp.Matrix(A)\n",
    "B=sp.Matrix(B)\n",
    "\n",
    "A*B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9c6cf8ac",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "af1b8d69",
   "metadata": {},
   "source": [
    "**<font size=5>18.</font>**用矩阵的分块求下列矩阵的逆矩阵.\n",
    "\n",
    "(1)$\\begin{pmatrix} 0 & 0 & 2 \\\\ 1 & 2 & 0 \\\\ 3 & 4 & 0 \\end{pmatrix}$；(2)$\\begin{pmatrix} 1 & 0 & 0 & 0 \\\\ 1 & 2 & 0 & 0 \\\\ 2 & 1 & 3 & 0 \\\\ 1 & 2 & 1 & 4 \\end{pmatrix}$；(3)$\\begin{pmatrix} 5 & 2 & 0 & 0 \\\\ 2 & 1 & 0 & 0 \\\\ 0 & 0 & 8 & 3 \\\\ 0 & 0 & 5 & 2\\end{pmatrix}$;\n",
    "\n",
    "(4)$\\begin{pmatrix} 1 & 1 & 0 & 0 & 0 \\\\ -1 & 3 & 0 & 0 & 0 \\\\ 0 & 0 & -2 & 0 & 0 \\\\ 0 & 0 & 0 & 1 & 1 \\\\ 0 & 0 & 0 & 0 & 2 \\end{pmatrix}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "c55f56a9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}0 & -2 & 1\\\\0 & \\frac{3}{2} & - \\frac{1}{2}\\\\\\frac{1}{2} & 0 & 0\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[  0,  -2,    1],\n",
       "[  0, 3/2, -1/2],\n",
       "[1/2,   0,    0]])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#(1)\n",
    "import numpy as np\n",
    "import sympy as sp\n",
    "A = np.array([[0,0,2],[1,2,0],[3,4,0]])\n",
    "\n",
    "A=sp.Matrix(A)\n",
    "\n",
    "A.inv()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "e32fbbcf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}1 & 0 & 0 & 0\\\\- \\frac{1}{2} & \\frac{1}{2} & 0 & 0\\\\- \\frac{1}{2} & - \\frac{1}{6} & \\frac{1}{3} & 0\\\\\\frac{1}{8} & - \\frac{5}{24} & - \\frac{1}{12} & \\frac{1}{4}\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[   1,     0,     0,   0],\n",
       "[-1/2,   1/2,     0,   0],\n",
       "[-1/2,  -1/6,   1/3,   0],\n",
       "[ 1/8, -5/24, -1/12, 1/4]])"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#(2)\n",
    "import numpy as np\n",
    "import sympy as sp\n",
    "A = np.array([[1,0,0,0],[1,2,0,0],[2,1,3,0],[1,2,1,4]])\n",
    "\n",
    "A=sp.Matrix(A)\n",
    "\n",
    "A.inv()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "b2f11e89",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}1 & -2 & 0 & 0\\\\-2 & 5 & 0 & 0\\\\0 & 0 & 2 & -3\\\\0 & 0 & -5 & 8\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[ 1, -2,  0,  0],\n",
       "[-2,  5,  0,  0],\n",
       "[ 0,  0,  2, -3],\n",
       "[ 0,  0, -5,  8]])"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#(3)\n",
    "import numpy as np\n",
    "import sympy as sp\n",
    "A = np.array([[5,2,0,0],[2,1,0,0],[0,0,8,3],[0,0,5,2]])\n",
    "\n",
    "A=sp.Matrix(A)\n",
    "\n",
    "A.inv()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "0ba142ef",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}\\frac{3}{4} & - \\frac{1}{4} & 0 & 0 & 0\\\\\\frac{1}{4} & \\frac{1}{4} & 0 & 0 & 0\\\\0 & 0 & - \\frac{1}{2} & 0 & 0\\\\0 & 0 & 0 & 1 & - \\frac{1}{2}\\\\0 & 0 & 0 & 0 & \\frac{1}{2}\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[3/4, -1/4,    0, 0,    0],\n",
       "[1/4,  1/4,    0, 0,    0],\n",
       "[  0,    0, -1/2, 0,    0],\n",
       "[  0,    0,    0, 1, -1/2],\n",
       "[  0,    0,    0, 0,  1/2]])"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#(4)\n",
    "import numpy as np\n",
    "import sympy as sp\n",
    "A = np.array([[1,1,0,0,0],[-1,3,0,0,0],[0,0,-2,0,0],[0,0,0,1,1],[0,0,0,0,2]])\n",
    "\n",
    "A=sp.Matrix(A)\n",
    "\n",
    "A.inv()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4617113e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "aef4c647",
   "metadata": {},
   "source": [
    "**<font size=5>19.</font>**设$A=\\begin{pmatrix} 3 & 4 & 0 & 0 \\\\ 4 & -3 & 0 & 0 \\\\ 0 & 0 & 2 & 0 \\\\ 0 & 0 & 2 & 2 \\end{pmatrix}$，求$\\left|\\begin{matrix}A^8\\end{matrix}\\right|$，及$A^8$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "bca04905",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 10000000000000000$"
      ],
      "text/plain": [
       "10000000000000000"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import sympy as sp\n",
    "\n",
    "import numpy as np\n",
    "def matrixPow(Matrix,n):\n",
    "    if(type(Matrix)==list):\n",
    "        Matrix=np.array(Matrix)\n",
    "    if(n==1):\n",
    "        return Matrix\n",
    "    else:\n",
    "        return np.matmul(Matrix,matrixPow(Matrix,n-1))\n",
    "\n",
    "A = np.array([[3,4,0,0],[4,-3,0,0],[0,0,2,0],[0,0,2,2]])\n",
    "B = matrixPow(A,8)\n",
    "\n",
    "B=sp.Matrix(B)\n",
    "\n",
    "B.det()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "73947454",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}625 & 0 & 0 & 0\\\\0 & 625 & 0 & 0\\\\0 & 0 & 16 & 0\\\\0 & 0 & 64 & 16\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[625,   0,  0,  0],\n",
       "[  0, 625,  0,  0],\n",
       "[  0,   0, 16,  0],\n",
       "[  0,   0, 64, 16]])"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A=sp.Matrix(A)\n",
    "A*A*A*A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6c0030eb",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "0089ee68",
   "metadata": {},
   "source": [
    "**<font size=5>20.</font>**用初等行变换求下列矩阵的逆矩阵.\n",
    "\n",
    "(1)$\\begin{pmatrix} 2 & 2 & 3 \\\\ 1 & -1 & 0 \\\\ -1 & 2 & 1 \\end{pmatrix}$；(2)$\\begin{pmatrix} 1 & 2 & 2 \\\\ 2 & 1 & -2 \\\\ 2 & -2 & 1 \\end{pmatrix}$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "869f661f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}1 & -4 & -3\\\\1 & -5 & -3\\\\-1 & 6 & 4\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[ 1, -4, -3],\n",
       "[ 1, -5, -3],\n",
       "[-1,  6,  4]])"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#(1)\n",
    "import numpy as np\n",
    "import sympy as sp\n",
    "A = np.array([[2,2,3],[1,-1,0],[-1,2,1]])\n",
    "\n",
    "A=sp.Matrix(A)\n",
    "\n",
    "A.inv()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "a53db40a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}\\frac{1}{9} & \\frac{2}{9} & \\frac{2}{9}\\\\\\frac{2}{9} & \\frac{1}{9} & - \\frac{2}{9}\\\\\\frac{2}{9} & - \\frac{2}{9} & \\frac{1}{9}\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[1/9,  2/9,  2/9],\n",
       "[2/9,  1/9, -2/9],\n",
       "[2/9, -2/9,  1/9]])"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#(2)\n",
    "import numpy as np\n",
    "import sympy as sp\n",
    "A = np.array([[1,2,2],[2,1,-2],[2,-2,1]])\n",
    "\n",
    "A=sp.Matrix(A)\n",
    "\n",
    "A.inv()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a1db005a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "3a8b7678",
   "metadata": {},
   "source": [
    "**<font size=5>21.</font>**设$A=\\begin{pmatrix} 1 & 2 \\\\ -1 & 0 \\end{pmatrix}$，又$f(x)=x^2-3x+2$，求$f(A)$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "62271d36",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}-2 & -4\\\\2 & 0\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[-2, -4],\n",
       "[ 2,  0]])"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import sympy as sp\n",
    "\n",
    "A = np.array([[1,2],[-1,0]])\n",
    "\n",
    "E=sp.eye(2)\n",
    "A =sp.Matrix(A)\n",
    "\n",
    "A*A-3*A+2*E"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "499e7394",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "ef2a2ab6",
   "metadata": {},
   "source": [
    "**<font size=5>22.</font>**当$t$为何值时？矩阵$A=\\begin{pmatrix} 1 & 0 & 2 & 3 \\\\ t & 1 & -1 & 2 \\\\ 0 & 1 & 3 & 8 \\end{pmatrix}$的秩为2."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "38f8db50",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 0$"
      ],
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import sympy as sp\n",
    "\n",
    "t=symbols('t')\n",
    "A = np.array([[0,2,3],[1,-1,2],[1,3,8]])\n",
    "A = sp.Matrix(A)\n",
    "\n",
    "A.det()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "4569fcff",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 2 t + 4$"
      ],
      "text/plain": [
       "2*t + 4"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import sympy as sp\n",
    "\n",
    "t=symbols('t')\n",
    "A = np.array([[1,0,2],['t',1,-1],[0,1,3]])\n",
    "A = sp.Matrix(A)\n",
    "\n",
    "A.det()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "ff8edfa2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 3 t + 6$"
      ],
      "text/plain": [
       "3*t + 6"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import sympy as sp\n",
    "\n",
    "t=symbols('t')\n",
    "A = np.array([[1,0,3],['t',1,2],[0,1,8]])\n",
    "A = sp.Matrix(A)\n",
    "\n",
    "A.det()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "27f8e228",
   "metadata": {},
   "source": [
    "所以$t=-2$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "4cfc1281",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(Matrix([\n",
       " [1, 0, 2, 3],\n",
       " [0, 1, 3, 8],\n",
       " [0, 0, 0, 0]]),\n",
       " (0, 1))"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#验证\n",
    "import numpy as np\n",
    "import sympy as sp\n",
    "\n",
    "A = np.array([[1,0,2,3],[-2,1,-1,2],[0,1,3,8]])\n",
    "A = sp.Matrix(A)\n",
    "\n",
    "A.rref()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "419c995e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "439d8834",
   "metadata": {},
   "source": [
    "**<font size=5>23.</font>**计算$\\begin{pmatrix} 0 & 1 & 0 \\\\ 1 & 0 & 0 \\\\ 0 & 0 & 1 \\end{pmatrix}^{2011}$$\\begin{pmatrix} 1 & 2 & 3 \\\\ 4 & 5 & 6 \\\\ 7 & 8 & 9 \\end{pmatrix}$$\\begin{pmatrix} 0 & 0 & 1 \\\\ 0 & 1 & 0 \\\\ 1 & 0 & 0 \\end{pmatrix}^{2010}$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "5694692b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}4 & 5 & 6\\\\1 & 2 & 3\\\\7 & 8 & 9\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[4, 5, 6],\n",
       "[1, 2, 3],\n",
       "[7, 8, 9]])"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import sympy as sp\n",
    "\n",
    "def A_n(A,n):\n",
    "    m = A.shape[0]\n",
    "    s = sp.eye(m)\n",
    "    for i in range(n):\n",
    "        s = s*A\n",
    "    return s\n",
    "    \n",
    "A = np.array([[0,1,0],[1,0,0],[0,0,1]])\n",
    "A = sp.Matrix(A)\n",
    "B = np.array([[1,2,3],[4,5,6],[7,8,9]])\n",
    "B = sp.Matrix(B)\n",
    "C = np.array([[0,0,1],[0,1,0],[1,0,0]])\n",
    "C = sp.Matrix(C)\n",
    "\n",
    "A_n(A,2011)*B*A_n(C,2010)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "05bef3ff",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "005e3c4f",
   "metadata": {},
   "source": [
    "**<font size=5>24.</font>**已知$A\\begin{pmatrix} 1 & 1 & 1 \\\\ 0 & 1 & 1 \\\\ 1 & 0 & 1 \\end{pmatrix}=\\begin{pmatrix} 1 & 2 & 3 \\\\ 4 & 5 & 6 \\end{pmatrix}$，求矩阵A."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "77322855",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}0 & 2 & 1\\\\3 & 2 & 1\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[0, 2, 1],\n",
       "[3, 2, 1]])"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import sympy as sp\n",
    "    \n",
    "B = np.array([[1,1,1],[0,1,1],[1,0,1]])\n",
    "B = sp.Matrix(B)\n",
    "\n",
    "C = np.array([[1,2,3],[4,5,6]])\n",
    "C = sp.Matrix(C)\n",
    "\n",
    "C*B.inv()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ad2f0702",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "d4381cf0",
   "metadata": {},
   "source": [
    "**<font size=5>25.</font>**设$A=\\begin{pmatrix} 1 & 1 & -1 \\\\ 0 & 1 & 1 \\\\ 0 & 0 & -1 \\end{pmatrix}$，求矩阵B，使$A^2-AB=E$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "75bbb375",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}0 & 2 & 1\\\\0 & 0 & 0\\\\0 & 0 & 0\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[0, 2, 1],\n",
       "[0, 0, 0],\n",
       "[0, 0, 0]])"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import sympy as sp\n",
    "    \n",
    "A = np.array([[1,1,-1],[0,1,1],[0,0,-1]])\n",
    "A = sp.Matrix(A)\n",
    "\n",
    "E=sp.eye(3)\n",
    "\n",
    "A.inv()*(A*A-E)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9763fd94",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "56584b39",
   "metadata": {},
   "source": [
    "**<font size=5>26.</font>**已知$AP=PB$，$B=\\begin{pmatrix} 1 & 0 & 0 \\\\ 0 & 0 & 0 \\\\ 0 & 0 & -1 \\end{pmatrix}$，$P=\\begin{pmatrix} 1 & 0 & 0 \\\\ 2 & -1 & 0 \\\\ 2 & 1 & 1 \\end{pmatrix}$，求$A$及$A^5$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "73390cc6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}1 & 0 & 0\\\\2 & 0 & 0\\\\6 & -1 & -1\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[1,  0,  0],\n",
       "[2,  0,  0],\n",
       "[6, -1, -1]])"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import sympy as sp\n",
    "    \n",
    "B = np.array([[1,0,0],[0,0,0],[0,0,-1]])\n",
    "B = sp.Matrix(B)\n",
    "\n",
    "P = np.array([[1,0,0],[2,-1,0],[2,1,1]])\n",
    "P = sp.Matrix(P)\n",
    "\n",
    "P*B*P.inv()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "26550d22",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "88a04d65",
   "metadata": {},
   "source": [
    "**<font size=5>27.</font>**解下列矩阵方程：\n",
    "\n",
    "(1)$A=\\begin{pmatrix} 0 & 2 & 1 \\\\ 2 & -1 & 3 \\\\ -3 & 3 & -4 \\end{pmatrix}$，$B=\\begin{pmatrix} 1 & 2 & 3 \\\\ 2 & -3 & 1 \\end{pmatrix}$，求$X$，使$XA=B$.\n",
    "\n",
    "(2)$AX=A+2X$，其中，$A=\\begin{pmatrix} 1 & -1 & 0 \\\\ 0 & 1 & -1 \\\\ -1 & 0 & 1 \\end{pmatrix}$，求X."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "6acbd26e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}2 & -1 & -1\\\\-4 & 7 & 4\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[ 2, -1, -1],\n",
       "[-4,  7,  4]])"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#(1)\n",
    "import numpy as np\n",
    "import sympy as sp\n",
    "    \n",
    "A = np.array([[0,2,1],[2,-1,3],[-3,3,-4]])\n",
    "A = sp.Matrix(A)\n",
    "\n",
    "B = np.array([[1,2,3],[2,-3,1]])\n",
    "B = sp.Matrix(B)\n",
    "\n",
    "B*A.inv()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "b43669d8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}0 & 1 & -1\\\\-1 & 0 & 1\\\\1 & -1 & 0\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[ 0,  1, -1],\n",
       "[-1,  0,  1],\n",
       "[ 1, -1,  0]])"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#(2)\n",
    "import numpy as np\n",
    "import sympy as sp\n",
    "    \n",
    "A = np.array([[1,-1,0],[0,1,-1],[-1,0,1]])\n",
    "A = sp.Matrix(A)\n",
    "\n",
    "E = sp.eye(3)\n",
    "\n",
    "(A-2*E).inv()*A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "57f06852",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "70878bf2",
   "metadata": {},
   "source": [
    "**<font size=5>30.</font>**设齐次线性方程组\n",
    "\n",
    "$\\left\\{\n",
    "\\begin{array}{rcrcrc}\n",
    "x_1  & - & 2x_2  & + & 2x_3  &   = 0 \\\\\n",
    "2x_1 & - & x_2 & + & \\lambda{x_3} &   = 0 \\\\\n",
    " x_1 & + & 2x_2 & - & x_3 &   = 0 \n",
    "\\end{array}\n",
    "\\right.$\n",
    "\n",
    "的系数矩阵为$A$，且存在三阶矩阵$B\\neq0$.求$\\lambda$的值，使得$AB=0$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "d44379d9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[7/4]"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import sympy as sp\n",
    "\n",
    "arr = np.array([[1,-2,2],[2,-1,'x'],[1,2,-1]])\n",
    "M = sp.Matrix(arr)\n",
    "x = sp.Symbol('x')\n",
    "\n",
    "sp.solve(M.det(),x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8b20bf20",
   "metadata": {},
   "source": [
    "因此，$\\lambda=\\frac{7}{4}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "72034b5c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
